﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace List2Code
{
    public class List2CodeEngine
    {
        public string GenerateCode(string list, string template)
        {
            return Parse(list, template);
        }

        private string Parse(string list, string template)
        {
            string[][] plist = StringToParam(list);
            string result = "";
            for (int i = 0; i < plist.Length; i++)
            {
                result += GenerateCodeFromTemplate(plist[i], template) + "\r\n\r\n";
            }
            return result;
        }

        private string[][] StringToParam(string list)
        {            
            string[] lines = list.Split(new string[1]{"\r\n"},StringSplitOptions.None);
            string[][] result = new string[lines.Length][];
            for (int i = 0; i < lines.Length; i++)
            {
                result[i] = lines[i].Split(',');
            }
            return result;
        }

        private string GenerateCodeFromTemplate(string[] param, string template)
        {
            string result = template;
            string var="";
            for (int i = 0; i < param.Length; i++)
            {
                var = "{"+i.ToString()+"}";
                result = result.Replace(var, param[i]);
            }
            return result;
            //return string.Format(template, param);
        }

    }
}
